﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Max Sum</title>
	<link href="styles/js-console.css" rel="stylesheet" />
</head>
<body>
    <div id="js-console"></div>
	<script src="scripts/js-console.js"></script>
    <script>
        function Solve(args) {
            var N = parseInt(args[0]);
            var i;
            var answer = 0;
            var arr = new Array(N);

            for (i = 1; i <= N; i++) {
                arr[i] = parseInt(args[i]);
            }

            var currentSum =parseInt(arr[1]);
            var maxSum = parseInt(arr[1]), maxLength = 1, maxIndex = 0;
            for (i = 1,currentIndex = 1; i <=N; i++)
            {
                currentSum += arr[i];

                if (arr[i] > currentSum)
                {
                    currentSum = arr[i];
                    currentIndex = i;
                }

            if (currentSum > maxSum)
                {
                    maxSum = currentSum;
                    maxIndex = currentIndex;
                    maxLength = i - currentIndex + 1;
                }
            }

            for (i = 0; i < maxLength; i++){
                answer+=arr[maxIndex + i];
            }
            return answer;
        }
    </script>
     <!--   //    var max =parseInt(array[1]);
        //    var maxEnd = parseInt(array[1]);
        //    var longSequence = 1;
        //    var currentSequence = 1;
        //    var start = 0;
        //    var startTemp = 0;

        //    for ( i = 1; i < N; i++)
        //    {
        //        if (array[i] + maxEnd > array[i])
        //        {
        //            maxEnd = array[i] + maxEnd;
        //            currentSequence++;
        //        }

        //        else
        //        {
        //            maxEnd = array[i];
        //            startTemp = i;
        //            currentSequence = 1;
        //        }

        //    if (maxEnd > max)
        //        {
        //            max = maxEnd;
        //            longSequence = currentSequence;
        //            start = startTemp;
        //        }
        //    }

        //for ( i = start; i < start + longSequence; i++)
        //{
        //    answer += array[i];
        //}
   -->

</body>
</html>
